package com.study.practice.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用于处理邮件发送
 */
@RestController
@RequestMapping("/mail")
public class MailCotroller {

    private static final Logger log = LoggerFactory.getLogger(MailCotroller.class);

    @Autowired
    private JavaMailSender mailSender;

    @Autowired
    Environment env;

    @RequestMapping("/send")
    public String sendMail() {
        log.info("发送邮件");
        SimpleMailMessage message = new SimpleMailMessage();
        //邮件发送方
        message.setFrom(env.getProperty("spring.mail.username"));
        //邮件接收方邮箱
        message.setTo("gxmhy2008@163.com","980019443@qq.com");
        //邮件标题
        message.setSubject("有新增留言信息通知");
        //发送邮件
        message.setText("测试邮件发送是否正常-test");
        //发送邮件
        mailSender.send(message);

        return "ok";
    }

}
